#include "eraser_tool.h"
#include "document.h"

#include <KMessageBox>
#include <QDebug>

#include "tool_commands.h"

Eraser::Eraser(QObject* parent): Tool(parent)
{
    m_erasing = false;
}

void Eraser::mousePressEvent(QMouseEvent* , Document*)
{
    m_erasing = true;
}

void Eraser::mouseMoveEvent(QMouseEvent* event, Document* document)
{
    if (!m_erasing) {
        return;
    }
    QPointF position_in_scene = document->mapToScene(event->pos());
    if (document->pageAt(position_in_scene)  == 0) {
        return;
    }
    QRectF pos = QRectF(position_in_scene.x() - 5, position_in_scene.y() - 5, 10, 10);
    QListIterator<QGraphicsItem*> iter(document->scene()->items(pos));

    QGraphicsItem *current;
    while (iter.hasNext()) {
        current = iter.next();
        // Erase everything that is not moved to the background.
        if (current->zValue() >= 0) {
            document->pushUndoCommand(new EraserUndoCommand(current));
        }
    }
}

void Eraser::mouseReleaseEvent(QMouseEvent* , Document*)
{
    m_erasing = false;
}
